<?php 
require_once('../vendor/autoload.php');
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
//证书目录
$path=dirname(__FILE__);
$path=$path.'/key/x/';
// 商户号
$merchantId = '1603745452';//商户号
$serial='1CFBD2C91C53F974E9AC2264A00B12A7421C030F';//证书系列
$appid='wxa41314fcbdd717e0';


$instance = Builder::factory([
    'mchid' => $merchantId,
    'serial' => $serial,
    'privateKey' => PemUtil::loadPrivateKey($path.'apiclient_key.pem'),
    'certs' => [//商户证书
        'YYYYYYYYYY' => PemUtil::loadCertificate($path.'key.pem')
    ],
    // APIv2密钥(32字节)--不使用APIv2可选
    'secret' => $serial,
    'merchant' => [// --不使用APIv2可选
        // 商户证书 文件路径 --不使用APIv2可选
        'cert' => $path.'apiclient_cert.pem',
        // 商户API私钥 文件路径 --不使用APIv2可选
        'key' => $path.'apiclient_key.pem',
    ],
]);



//用户参数
$detail=[
'out_detail_no' =>'aaa131',
'transfer_amount' =>30,
'transfer_remark'=>'转账',
'openid'=>'o9kl55s1B4VtgXvCF5CiZeaSAJ5A'
];

try {
    $resp = $instance
    ->chain('v3/transfer/batches')
    ->post(['json' => [
        'appid'        => 'wxa41314fcbdd717e0',
        'out_batch_no' => 'aaa12223a4',
        'batch_name'        => '转账到零钱',
        'batch_remark'  => '转账',
        'total_amount'   => 30,
        'total_num'   => 1,
        'transfer_detail_list'=> [$detail],
    ]]);

    echo $resp->getStatusCode(), PHP_EOL;
    echo $resp->getBody(), PHP_EOL;
} catch (\Exception $e) {
    // 进行错误处理
   // echo $e->getMessage(), PHP_EOL;
    
    
    if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
        $r = $e->getResponse();

        //echo $r->getStatusCode() . ' ' . $r->getReasonPhrase(), PHP_EOL;
       // echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
        $err=json_decode($r->getBody(),1);
        die($err['message']);
        
    }
   // echo $e->getTraceAsString(), PHP_EOL;
}


?>